package com.gexiaobao.pigeon.ui.adapter

import android.graphics.Paint
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.AuctionOrderListResponse
import com.gexiaobao.pigeon.databinding.ItemAuctionOrderBinding

/**
 * @Author      : hxw
 * @Date        : 2024/11/29 9:18
 * @Describe    :
 */
class AdapterAuctionOrder(data: ArrayList<AuctionOrderListResponse>) :
    BaseQuickAdapter<AuctionOrderListResponse, DataBindBaseViewHolder>(R.layout.item_auction_order, data) {

    private var mOnItemClickListener: OnOrderGoodsClickListener? = null

    override fun convert(holder: DataBindBaseViewHolder, item: AuctionOrderListResponse) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemAuctionOrderBinding
        binding.data = item
        binding.executePendingBindings()

        val tvAllOrderItemPhone = holder.getView<AppCompatTextView>(R.id.tvAllOrderItemPhone)
        tvAllOrderItemPhone.paint.flags = Paint.UNDERLINE_TEXT_FLAG//设置下划线

        holder.setGone(R.id.llTransportInfo, item.shippingWay.isEmpty() && item.shippingPrice == 0)
        holder.setGone(R.id.tvTransportationCost, item.shippingPrice == 0)
        holder.setText(R.id.tvTransportationCost, "运费:" + item.shippingPrice.toString() + "元")
        val phoneNum = item.phoneNo
        if (phoneNum.isNotEmpty() && phoneNum.length >= 8) {
            val result1 = phoneNum.substring(0, 3)
            val result2 = phoneNum.substring(7, phoneNum.length)
            holder.setText(R.id.tvAllOrderItemPhone, "$result1****$result2")
        }

        val mOrderState = item.orderStatus
        holder.setVisible(R.id.tvTotalPrice, mOrderState == 2)
        holder.setVisible(R.id.tvFeePrice, mOrderState == 2)
        holder.setVisible(R.id.tvOrderAuctionPay, mOrderState == 2 || mOrderState == 3 || mOrderState == 4)

        val btnContent = when (mOrderState) {
            2 -> "去付款"
            3, 4 -> "确认收货"
            else -> ""
        }
        holder.setText(R.id.tvOrderAuctionPay, btnContent)

        if (mOrderState == 2 || mOrderState == 3 || mOrderState == 4) {
            holder.setGone(R.id.llGoToPayment, false)
        } else {
            holder.setGone(R.id.llGoToPayment, true)
        }

        /** 1-未设置运费 2-已设置运费待付款 3-已付款 4-已发货 5-已收货 6-已评价 7-售后中 8-已退款 9-已退货 10-已退货退款 11-已付款未付保证金 */
        val orderDesc = when (mOrderState) {
            1 -> "卖方正根据收货地址设置运费"
            2 -> "等待付款"
            3 -> "卖方正在准备发货"
            4 -> "卖方已发货,运输中"
            5 -> "已收货"
            else -> ""
        }
        holder.setText(R.id.tvSetShippingPrice, orderDesc)

        val address = item.shippingAddress.ifEmpty { "运输方式以及费用由买卖双方自行商议" }
        holder.setText(R.id.tvOrderTransportConditionDesc, address)

        holder.itemView.apply {
            binding.recyclerView.adapter = null
            if (item.orderGoods.isEmpty()) {
                holder.setGone(R.id.recyclerView, true)
                return
            }
            holder.setGone(R.id.recyclerView, false)
            val mAdapter = AdapterOrderGoods(item.orderGoods)
            binding.recyclerView.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
                adapter = mAdapter
            }

            mAdapter.setOnItemClickListener { _, _, position ->
                mOnItemClickListener?.onItemClick(mAdapter.data[position].goodId, mAdapter.data[position].t7lUrl)
            }
        }
    }

    fun setOnOrderGoodsClickListener(listener: OnOrderGoodsClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnOrderGoodsClickListener {
        fun onItemClick(goodId: Int, coverImage: String)
    }
}